//
//  RegisterSaveViewController.m
//  tuanGG
//
//  Created by 微他 on 15/9/25.
//  Copyright (c) 2015年 weita. All rights reserved.
//
#define TEXT_NUM 8
#import "RegisterSaveViewController.h"
#import "CustomDialogView.h"
#import "HomeViewController.h"
#import "CustomAlertView.h"
#import "WJCityPikerView.h"
#import "TabBarController.h"
@interface RegisterSaveViewController ()<UITextFieldDelegate>
{
    UIView * _saveBackView;
    UILabel * _saveTitle;
    WJCityPikerView * _rgisterCity;
    UIView * _cityView;
    UITapGestureRecognizer * _cityTap;
}
@property (nonatomic,strong)UITextField * userName;
@property (nonatomic,strong)UITextField * bsName;
@property (nonatomic,strong)UILabel * bsCitylabel;
@property (nonatomic,strong)UITextField * bsAdress;

@property (nonatomic,strong)UIButton * registerBtn;
@property (nonatomic,strong)NSString * provice;//省
@property (nonatomic,strong)NSString * cityStr;//市级
@property (nonatomic,strong)NSString * districtStr;//区
@end

@implementation RegisterSaveViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}
- (instancetype)initWithinforPhone:(NSString *)phone psw:(NSString *)password authcode:(NSString *)authCodes
{
    self = [super init];
    if (self) {
        
        _phoneNum = [phone copy];
        _passWord = [password copy];
        _code = [authCodes copy];
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:nil];;
    [self addnavigationTitle:@"注册信息" navColor:@"4fd2be"];
    self.view.backgroundColor=UIColorFromRGB(base_gary_bg);
    
    [self lqkintRegisterSaveInit];
}

- (void)lqkintRegisterSaveInit
{
    _saveBackView = [[UIView alloc]initWithFrame:CGRectMake(15, 15, ScreenWidth - 30, 180)];
    [_saveBackView.layer setCornerRadius:5.0];
    _saveBackView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_saveBackView];
    
    NSArray * titleName = @[@"商家姓名",@"商家名称",@"所在地区",@"详细地址"];
    for (int i = 0; i < titleName.count; i ++) {
        
        _saveTitle = [[UILabel alloc]initWithFrame:CGRectMake(10, 7.5 +  i * 45, 80, 30)];
        _saveTitle.text = titleName[i];
        _saveTitle.textColor = [UIColor grayColor];
        _saveTitle.font = BOLD_FONT(15);
        [_saveBackView addSubview:_saveTitle];
        
        if (i == 0) {
            
            continue;
        }
        else
        {
            UIView * lineView = [Common line:CGRectMake(0, 45 * i, ScreenWidth-30, 1)];
            lineView.backgroundColor = [Common lqkinLineWithGray:@"c8c8c8" alpha:0.5];
            [_saveBackView addSubview:lineView];
        }
    }
    
    
    _registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _registerBtn.frame = CGRectMake(15, CGRectGetHeight(_saveBackView.bounds) + 55 + 15, ScreenWidth- 30, 40);
    [_registerBtn.layer setCornerRadius:5.0];
    [_registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    [_registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal
     ];
    [_registerBtn setBackgroundColor:[UIColor colorWithHexString:@"33ccbf"]];
    [_registerBtn addTarget:self action:@selector(registerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    _registerBtn.titleLabel.font = BOLD_FONT(15);
    [self.view addSubview:_registerBtn];
    
    
    //商家姓名
    _userName = [[UITextField alloc]initWithFrame:CGRectMake(90,0 , CGRectGetWidth(_saveBackView.bounds) - 100, 45)];
    _userName.textAlignment = NSTextAlignmentRight;
    _userName.font = BOLD_FONT(15);
    [_saveBackView addSubview:_userName];
    
    
    //商家名称
    _bsName = [[UITextField alloc]initWithFrame:CGRectMake(90,45 , CGRectGetWidth(_saveBackView.bounds) - 100, 45)];
    _bsName.textAlignment = NSTextAlignmentRight;
    _bsName.font = BOLD_FONT(15);
    _bsName.delegate = self;
    _bsName.placeholder = @"例黄记煌三汁焖锅（仅限8个字)";
    [_saveBackView addSubview:_bsName];
    
    //所在地区
    _bsCitylabel = [[UILabel alloc]initWithFrame:CGRectMake(90,90 , CGRectGetWidth(_saveBackView.bounds) - 100, 45)];
    _bsCitylabel.textAlignment = NSTextAlignmentRight;
    _bsCitylabel.font = BOLD_FONT(15);
    //    _bsCity.placeholder = @"例黄记煌三汁焖锅（九方购物)";
    _bsCitylabel.userInteractionEnabled = YES;
    [_saveBackView addSubview:_bsCitylabel];
    
    _cityTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapgestureCity:)];
    [_bsCitylabel addGestureRecognizer:_cityTap];
    //所在地址
    _bsAdress = [[UITextField alloc]initWithFrame:CGRectMake(90,135 , CGRectGetWidth(_saveBackView.bounds) - 100, 45)];
    _bsAdress.textAlignment = NSTextAlignmentRight;
    _bsAdress.font = BOLD_FONT(15);
    [_saveBackView addSubview:_bsAdress];
    
    
    //选择城市
    _cityView = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight, ScreenWidth, ScreenHeight-64)];
    _cityView.backgroundColor = RGBACOLOR(138, 138, 136, 0.5);
    //    _cityView.backgroundColor = [UIColor redColor];
    _cityView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapPressed = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(buttonOfPressed)];
    [_cityView addGestureRecognizer:tapPressed];
    [self.view addSubview:_cityView];

    _rgisterCity = [[WJCityPikerView alloc]initWithProvinces:[DatabaseSql queryAllProvinces] city:[DatabaseSql findprovid:@"1" cityID:@"0"] distrct:nil];
    _rgisterCity.isContains = NO;
    _rgisterCity.backgroundColor = RGBACOLOR(236, 235, 232, 1);
    //     _wjpick.backgroundColor = [UIColor redColor];
    //    _rgisterCity.backgroundColor = [UIColor purpleColor];
    [_cityView addSubview:_rgisterCity];
}


#pragma mark - <TextFieldDelegate>
- (void)TextFieldDidChange:(NSNotification *)notification
{
    UITextField * text = (UITextField *)notification.object;
    if (text == _bsName) {
        
        if (text.text.length > TEXT_NUM) {
            text.text = [text.text substringToIndex:TEXT_NUM];
        }
    }
}


//注册
- (void)registerBtnClick:(UIButton *)sender
{
    [self delectedTxtisNull];
}

- (void)delectedTxtisNull
{
    if ([_userName.text isEqualToString:@""]) {
        
        [CustomAlertView alterSureMsg:@"请输入用户名" delegate:nil];
    }else
    {
        if ([_bsName.text isEqualToString:@""]) {
            
            [CustomAlertView alterSureMsg:@"请输入商家名称" delegate:nil];
        }
        else
        {
            if ([_bsCitylabel.text isEqualToString:@""]) {
                
                [CustomAlertView alterSureMsg:@"请输入用户名" delegate:nil];
            }
            else
            {
                if ([_bsAdress.text isEqualToString:@""]) {
                    [CustomAlertView alterSureMsg:@"请输入详细地址" delegate:nil];
                }
                else
                {
                    
                    [self registerNetWorking];
                }
            }
        }
    }
}

#pragma mark - <注册网络请求>
- (void)registerNetWorking
{
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    [params setObject:@"1" forKey:@"mode"];
    [params setObject:_phoneNum forKey:@"tel"];
    [params setObject:_passWord forKey:@"psw"];
    //    [params setObject:_code forKey:@"vcode"];//验证码
    [params setObject:_bsName.text forKey:@"merchname"];//商家名称
    [params setObject:_userName.text forKey:@"ownername"];//拥有者名称
    [params setObject:_provice forKey:@"province"];         //省市
    [params setObject:_cityStr forKey:@"city"];             //城市
    [params setObject:_districtStr forKey:@"district"];     //区
    if ([_districtStr isEqualToString:@""]) {
        [params setObject:_provice forKey:@"province"];         //省市
        [params setObject:_provice forKey:@"city"];             //城市
        [params setObject:_cityStr forKey:@"district"];         //区
    }else {

    }

    [params setObject:_bsAdress.text forKey:@"address"];//详细地址
    
    [GLAFRequest startRequest:[NSString stringWithFormat:@"%@%@",baseurl_url,REGISTER_url] defaultdialog:YES paramsDic:params handleSuccess:^(id sta, NSDictionary *paramsDic) {
        
        NSLog(@"%@",sta);
        if ([sta[@"code"] integerValue] == 1) {
            CustomDialogView * dialog = [[CustomDialogView alloc]initWithTitle:@"" message:@"注册成功,我们会在3-5个工作日内联系您" buttonTitles:@"确定", nil];
            [dialog showWithCompletion:^(NSInteger selectIndex) {
                [UserInfo sharedUserInfo].loginStatus = YES;
                [UserInfo sharedUserInfo].logo = YES;
                [UserInfo sharedUserInfo].uid = [sta[@"a_memberupdateobj"][@"uid"]integerValue];;
                TabBarController* leadVc = [[TabBarController alloc]init];
                [self presentViewController:leadVc animated:YES completion:nil];
            }];
        }
        
        
    } netWorkFail:^(NSDictionary *paramsDic) {
        
        [CustomAlertView alterSureMsg:@"网络连接异常,请检查网络连接" delegate:nil];
        
    }];
    
}
#pragma mark - <选择城市>

- (void)buttonOfPressed {
    [UIView animateWithDuration:0.2 animations:^{
        _cityView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, ScreenHeight-64);
    }];
}

- (void)tapgestureCity:(UITapGestureRecognizer *)tap
{
    [UIView animateWithDuration:0.2 animations:^{
        _cityView.hidden = NO;
        _cityView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight-64);
    }];
    [_rgisterCity setModelBlock:^(WJCityPikerModel *model) {



        NSLog(@"%@,%@,%@,%@,%@,%@",model.provinceName,model.provinceId,model.cityId,model.cityName,model.disId,model.disName);
        if (!model.disName) {
            model.disName = @"";
        }
        _provice = model.provinceName;
        _cityStr = model.cityName;
        _districtStr = model.disName;
        _bsCitylabel.text = [NSString stringWithFormat:@"%@%@%@",model.provinceName,model.cityName,model.disName];
        [UIView animateWithDuration:0.2 animations:^{
            
            _cityView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, ScreenHeight-64);
        }];
        
        [self.view endEditing:YES];
        
    }];
    [_rgisterCity setBackBlock:^{
        _cityView.hidden = YES;
//        [self.view endEditing:YES];

    }];
    
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:YES];
}
@end
